package router

import (
	"crow-k8s/src/config"
	"crow-k8s/src/service"
	"github.com/gin-gonic/gin"
	"github.com/gorilla/websocket"
	"github.com/toolkits/pkg/logger"
	"net/http"
)

// GetPodList 查看pod列表
// @Summary 查看pod列表
// @Description 查看pod列表
// @Tags Pod
// @Param namespace_name query string true "namespace名字"
// @Success 200 {object} response.Response{data=v1.PodList} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/pod/list [get]
// @Security Bearer
func GetPodList(c *gin.Context) {
	namespaceName := c.Query("namespace_name")
	deploymentList, err := service.GetPodList(config.K8sClientSet, namespaceName)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, deploymentList)
}

// GetPod 查看指定pod
// @Summary 查看指定pod
// @Description 查看指定pod
// @Tags Pod
// @Param namespace_name query string true "namespace名字"
// @Param pod_name query string true "pod名字"
// @Success 200 {object} response.Response{data=v1.Pod} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/pod [get]
// @Security Bearer
func GetPod(c *gin.Context) {
	namespaceName := c.Query("namespace_name")
	podName := c.Query("pod_name")
	deploymentInfo, err := service.GetPod(config.K8sClientSet, namespaceName, podName)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, deploymentInfo)
}

// DeletePod 删除pod
// @Summary 删除pod
// @Description 删除pod
// @Tags Pod
// @Accept  application/json
// @Product application/json
// @Param data body service.DeletePodRequest true "删除pod的信息"
// @Success 200 {object} response.Response{data=string} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/pod [delete]
// @Security Bearer
func DeletePod(c *gin.Context) {
	var request service.DeletePodRequest
	err := c.Bind(&request)
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	err = service.DeletePod(config.K8sClientSet, request)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, "删除成功")
}

var upgrader = websocket.Upgrader{
	// 解决跨域问题
	CheckOrigin: func(r *http.Request) bool {
		return true
	},
}

func GetPodLogs(c *gin.Context) {
	//升级websocket接口
	ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)

	namespaceName := c.Query("namespace_name")
	podName := c.Query("pod_name")

	//接收消息的携程
	go func() {
		for {
			_, message, err := ws.ReadMessage()
			if err != nil {
				logger.Error("接口断开:", err)
				ws.Close()
				return
			}
			logger.Info(string(message))
		}
	}()

	//获取日志
	err = service.GetPodLogs(config.K8sClientSet, namespaceName, podName, ws)
	for err != nil {
		SetErrDefault(c, err)
		return
	}

}
